/////////////////////////////
//         Seeds           //
//      2006 BenheM        //
//  use only with remorse  //
/////////////////////////////
// http://benhem.com/games/seeds/
float fade=.25;
float splitChance=1;
float bendChance=14;
float growSpeed=.8;
float bendAmount=PI/8;
float persist=10;
Vector seeds=new Vector();
PFont font;

void setup(){
  size(400,400);
  colorMode(HSB,100);
  background(5);
  //smooth();
  font = loadFont("ArialNarrow-48.vlw"); 
  textFont(font, 48); 
  text("Seeds", 15, 50);
  fill(#ffeefc,25);
  text("Click anywhere\n to start\n the application", 15, 80);
}

void draw(){
  if (seeds.size() > 0) for (int i=0; i<seeds.size(); i++){
    ((seed)seeds.get(i)).update();
  }
}

void mouseReleased(){
  seeds.add(new seed(mouseX,mouseY,random(-PI,PI),100));
}

class seed{
  float prevx,prevy,xpos,ypos,angle,opacity;
  seed(float _xpos, float _ypos, float _angle, float _opacity){
    xpos=_xpos; ypos=_ypos; angle=_angle;opacity=_opacity;
  }
  void update(){
    prevx=xpos; prevy=ypos;
    xpos+=(growSpeed * cos(angle));
    ypos+=(growSpeed * sin(angle));
    if (random(100) < bendChance) angle+=random(-bendAmount,bendAmount);
    if (random(100) < splitChance) seeds.add(new seed(xpos,ypos,angle+random(-bendAmount,bendAmount),opacity+random(persist)));
    opacity-=fade;
    if (opacity<0) seeds.remove(this);
    render();
  }
  void render(){
    stroke(100,opacity);
    line(prevx,prevy,xpos,ypos);
  }
}

void keyReleased(){
  switch(key){
    case('c'): seeds.clear(); background(0); break;
    case(' '): seeds.clear(); break;
  }
}
